﻿Public Class TraCuuHK

    Private f_HanhKhach As ThemHanhKhach
    Private bustracuu As New BUS.TraCuuKH
    Private dtotracuu As New DTO.TraCuuHK
    Private f_edithk As New EditHanhKhach

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If IsNothing(f_HanhKhach) Then
            f_HanhKhach = New ThemHanhKhach
        End If
        f_HanhKhach.ShowDialog()
    End Sub

    Private Sub TraCuuHK_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.Hide()
        e.Cancel = True
    End Sub

    Private Sub TraCuuHK_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dtotracuu.dtg = bustracuu.dtload()
        dgkhachhang.DataSource = dtotracuu.dtg.Tables(0)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim res = MessageBox.Show("Bạn có chắc muốn xóa không!", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question)
        If res = DialogResult.Yes Then
            Dim dgcell As DataGridViewCell
            For Each dgcell In dgkhachhang.SelectedCells
                Dim chek As Integer = bustracuu.delhk(dgkhachhang.Item(0, dgcell.RowIndex).Value)
                If chek = Nothing Then
                    MessageBox.Show("Không thể xóa. Xin bạn kiểm tra lại ràng buộc từ các bảng dữ liệu khác", "Lỗi", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Stop)
                    dgkhachhang.DataSource = bustracuu.dtload().Tables(0)
                    Exit Sub
                End If
            Next
            dgkhachhang.DataSource = bustracuu.dtload().Tables(0)
        End If
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Hide()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        If dgkhachhang.SelectedCells.Count <> 1 Then
            MessageBox.Show("Vui lòng chọn 1 hành khách để sửa thông tin.", "Thông báo", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error)
            Exit Sub
        End If
        Dim mahkeditt As Integer = dgkhachhang.Item(0, dgkhachhang.SelectedCells(0).RowIndex).Value
        f_edithk.Label5.Text = mahkeditt.ToString
        f_edithk.ShowDialog()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dtotracuu.hotenhk = txthoten.Text
        dtotracuu.dthk = txtdienthoai.Text
        dtotracuu.cmndhk = txtcmnd.Text
        Dim res As DataSet = bustracuu.Findhk(dtotracuu)
        If res.Tables(0).Rows.Count = 0 Then
            MessageBox.Show("Không tìm thấy khách hàng theo yêu cầu!", "Lỗi", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error)
                Exit Sub
        End If
        dgkhachhang.DataSource = res.Tables(0)
    End Sub

    Private Sub TraCuuHK_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        dtotracuu.dtg = bustracuu.dtload()
        dgkhachhang.DataSource = dtotracuu.dtg.Tables(0)
    End Sub
End Class